package tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;

import static org.testng.Assert.*;
import static org.testng.Assert.assertTrue;

import org.testng.*;
import org.testng.annotations.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class DeleteMovie extends tests.supers.TestBaseOld {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Test
  public void testDeleteMovie() throws Exception {
    driver.get(baseUrl + "/php4dvd/");
    driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys("admin");
    driver.findElement(By.name("password")).clear();
    driver.findElement(By.name("password")).sendKeys("admin");
    driver.findElement(By.name("submit")).click();
//to find all movies
    driver.findElement(By.id("q")).clear();
    driver.findElement(By.id("q")).sendKeys(Keys.RETURN);

    String elementText="";
    try{
    	elementText = driver.findElement(By.cssSelector("div#content")).getText();
    	Assert.assertFalse(elementText.contains("No movies where found."));

    }
    catch(Exception e){
    	//System.err.println("Seems as no movies to delete");
    	driver.findElement(By.linkText("Log out")).click();
        assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));
    	Assert.fail("Seems as no movies to delete");
    }

    WebElement selectedMovie=driver.findElement(By.cssSelector("div.title"));
    String movieTitle = selectedMovie.getText();
    selectedMovie.click();

    driver.findElement(By.cssSelector("img[alt=\"Remove\"]")).click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to remove this[\\s\\S]$"));
    //driver.findElement(By.id("errorTryAgain")).click();
    driver.navigate().refresh();
    driver.findElement(By.id("q")).clear();
    driver.findElement(By.id("q")).sendKeys(movieTitle+ Keys.RETURN);

    try {
    	elementText = driver.findElement(By.cssSelector("div#content")).getText();
    	Assert.assertTrue(elementText.contains("No movies where found."));
    	System.out.println("Movie was deleted successfully");
    	}
    	catch (Exception e) {
    	//System.err.println("Seems as the movie wasn't deleted or there is another movie with same title: "+ movieTitle);

    	driver.findElement(By.linkText("Log out")).click();
        assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));

        Assert.fail("Seems as the movie wasn't deleted or there is another movie with same title: "+ movieTitle);
    	}

    driver.findElement(By.linkText("Log out")).click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));
  }


  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

